package inits

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"user-srv/basic/config"
	"user-srv/basic/global"
	"user-srv/internal/model"
)

func InitMysql() {
	var err error
	mysqlConfig := config.Config.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8&parseTime=True",
		mysqlConfig.User,
		mysqlConfig.Password,
		mysqlConfig.Host,
		mysqlConfig.Port,
		mysqlConfig.Database,
	)

	global.DB, err = gorm.Open(mysql.Open(dsn))
	if err != nil {
		panic(err)
	}

	fmt.Println("MySQL数据库连接成功")
	err = global.DB.AutoMigrate(&model.User{})
	if err != nil {
		panic(err)
	}
	fmt.Println("数据表迁移成功")
	db, err := global.DB.DB()
	if err != nil {
		panic(err)
	}

	fmt.Println("连接池连接成功")

	db.SetMaxIdleConns(10)
	db.SetMaxOpenConns(100)

}
